package ai4games_tests.soccer;

import game2dai.World;
import game2dai.entities.BaseEntity;
import game2dai.fsm.State;
import game2dai.fsm.Telegram;
import game2dai.steering.SBF;

public class PlayerSupportAttacker extends State implements Constants {


	private static PlayerSupportAttacker instance = null;
	
	public static PlayerSupportAttacker state(){
		if(instance == null){
			instance = new PlayerSupportAttacker();
			instance.name = "Support Attacker";
		}
		return instance;
	}

	@Override
	public void enter(BaseEntity owner) {
		PlayerBase player = (PlayerBase) owner;
		// Find the best supporting position
		player.AP().arriveOn(player.team.getBestSupportngSpot(), SBF.FAST);
	}

	@Override
	public void execute(BaseEntity owner, double deltaTime, World world) {
		PlayerBase player = (PlayerBase) owner;
		// If team is not in control then go to current region
		if(!player.team.isInControl() || player != player.team.getSupportingPlayer()){
			player.FSM().changeState(PlayerReturnToRegion.state());
			return;
		}
		// Update the best supporting position if required
		player.targetPos.set(player.team.getBestSupportngSpot());
		player.AP().arriveOn(player.targetPos, SBF.FAST);
		// See if this player has a possible shot at goal
		if(player.team.canShoot(player.pos(), MaxShootingForce, null)){
			player.team.requestPass(player);
		}
		if(player.isAtTarget()){
			player.AP().arriveOff();
			player.velocity(0, 0);
			player.trackBall();
			if(!player.isThreatened()){
				player.team.requestPass(player);			
			}
		}
		else if(!player.AP().isArriveOn()){
			player.AP().arriveOn();
		}
	}

	@Override
	public void exit(BaseEntity owner) {
		PlayerBase player = (PlayerBase) owner;
		player.team.setSupportingPlayer(null);
		player.AP().arriveOff();
	}

	@Override
	public boolean onMessage(BaseEntity owner, Telegram tgram) {
		return false;
	}

}
